package com.fruit.controller.wap.lightpay;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fruit.constants.PayConstant;
import com.fruit.controller.base.BaseController;
import com.fruit.pojo.pay.TransactionFlow;
import com.fruit.resolver.DefiniteException;
import com.fruit.service.lightpay.LightPayManager;
import com.fruit.service.pay.PayManager;
import com.fruit.util.ResultInfo;



@Controller
@RequestMapping(value = "/wap/result")
public class LightPayResultController extends BaseController {

	
	@Autowired
	private LightPayManager lightPayService;

	@Value("${domain}")
	private String domain;
	
	@RequestMapping(method = { RequestMethod.POST,RequestMethod.GET })
	public ModelAndView lightPayCashier (String code, HttpServletResponse response, HttpServletRequest request) {
		this.logBefore(logger, "支付结果");
		ModelAndView view = this.getModelAndView();

		Map<String, Object> resMap = lightPayService.getLightpayCashierByCode(code);
		
		ResultInfo ri = (ResultInfo)resMap.get("result");
		if(ResultInfo.ERROR.equals(ri.getState()) ){
			throw new DefiniteException(ri.getMessage());
		}
		
		
		TransactionFlow tf = (TransactionFlow)resMap.get("tf");
		if(PayConstant.State.pay.equals(tf.getState())){
			
			view.addObject("state", "01");
			view.addObject("price", tf.getPayPrice());
		}else{
			view.addObject("state", "00");
		}
		
		if(PayConstant.Type.consume.equals(tf.getType())){
			view.addObject("type", tf.getType());
			view.setViewName("lightpay/result");
		}
		
		
		return view;
	}
	
	
	
}
